package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.device.dao.AdminDao;
import com.zretc.device.dao.Device_AdminDao;
import com.zretc.device.dao.UserDao;
import com.zretc.device.entity.Admin;
import com.zretc.device.entity.Device_Admin;
import com.zretc.device.entity.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtusername;
	private JTextField txtusercode;
	private JPasswordField txtpassword;
	private JPasswordField txtpassconfirm;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UserAddDialog dialog = new UserAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UserAddDialog() {
		setBounds(100, 100, 692, 490);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("新增用户");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(254, 32, 182, 47);
		contentPanel.add(label);

		JComboBox txtCB = new JComboBox();
		txtCB.setModel(new DefaultComboBoxModel(new String[] { "普通员工", "仓库员工", "管理员" }));
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setBounds(280, 111, 250, 30);
		contentPanel.add(txtCB);

		JLabel label_4 = new JLabel("请选择用户类型");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(74, 105, 181, 38);
		contentPanel.add(label_4);

		txtusername = new JTextField();
		txtusername.setFont(new Font("宋体", Font.PLAIN, 25));
		txtusername.setColumns(10);
		txtusername.setBounds(280, 154, 250, 30);
		contentPanel.add(txtusername);

		JLabel label_1 = new JLabel("用 户 名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(146, 154, 120, 30);
		contentPanel.add(label_1);

		txtusercode = new JTextField();
		txtusercode.setFont(new Font("宋体", Font.PLAIN, 25));
		txtusercode.setColumns(10);
		txtusercode.setBounds(280, 197, 250, 30);
		contentPanel.add(txtusercode);

		JLabel label_2 = new JLabel("用户昵称");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(146, 197, 120, 30);
		contentPanel.add(label_2);

		JLabel label_3 = new JLabel("密    码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(146, 240, 120, 30);
		contentPanel.add(label_3);

		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 取值
				String txt = txtCB.getSelectedItem() + "";
				String username = txtusername.getText();
				String usercode = txtusercode.getText();
				char[] tpassword = txtpassword.getPassword();
				// 转型
				String password = new String(tpassword);
				// 确认密码
				char[] tpasscon = txtpassconfirm.getPassword();
				String passconfirm = new String(tpasscon);

				if (username.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "用户名不能为空");
				} else if (usercode.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "用户昵称不能为空");
				} else if (password.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				} else if (passconfirm.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				} else {
					//如果第一次和第二次密码都输入一样
					if (password.equals(passconfirm)) {
						if (txt.equals("普通员工")) {
							// 创建对象
							UserDao ud = new UserDao();
							User user = new User(username, usercode, password);
							// 添加
							boolean flag = ud.addUser(user);
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "添加成功");
							} else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "添加失败", "错误", JOptionPane.ERROR_MESSAGE);
							}

						} else if (txt.equals("仓库员工")) {
							Device_AdminDao dad = new Device_AdminDao();
							Device_Admin da = new Device_Admin(username, usercode, password);
							// 添加
							boolean flag = dad.addDevAdmin(da);
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "添加成功");
							} else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "添加失败", "错误", JOptionPane.ERROR_MESSAGE);
							}
						} else if (txt.equals("管理员")) {
							AdminDao ad = new AdminDao();
							Admin admin = new Admin(username, usercode, password);
							boolean flag = ad.addAdmin(admin);
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "添加成功");
							} else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "添加失败", "错误", JOptionPane.ERROR_MESSAGE);
							}

						}

					}else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "密码错误", "错误", JOptionPane.ERROR_MESSAGE);
					}

				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(380, 349, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtusername.setText("");
				txtusercode.setText("");
				txtpassword.setText("");
				txtpassconfirm.setText("");
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(166, 349, 150, 38);
		contentPanel.add(button_1);

		txtpassword = new JPasswordField();
		txtpassword.setBounds(280, 244, 250, 30);
		contentPanel.add(txtpassword);

		txtpassconfirm = new JPasswordField();
		txtpassconfirm.setBounds(280, 293, 250, 30);
		contentPanel.add(txtpassconfirm);

		JLabel label_5 = new JLabel("确认密码");
		label_5.setFont(new Font("宋体", Font.PLAIN, 25));
		label_5.setBounds(146, 289, 120, 30);
		contentPanel.add(label_5);
		// 居中
		setLocationRelativeTo(null);
	}
}
